﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisWeb.src;
using CocisModel;

namespace CocisWeb.admin
{
    public partial class ManageUser : BasePage
    {
        RoleDAL roleDAL = new RoleDAL();
        DeptDAL deptDAL = new DeptDAL();
        StaffDAL staffDAL = new StaffDAL();
        MakeMD5String md5 = new MakeMD5String();
        LogsDAL logs = new LogsDAL();
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
            }
        }
        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        public void BindDropDownList()
        {
            this.DropDownListDept.DataSource = deptDAL.GetAllDept();
            this.DropDownListDept.DataTextField = "department";
            this.DropDownListDept.DataValueField = "deptId";
            this.DropDownListDept.DataBind();

            this.DropDownListRole.DataSource = roleDAL.GetAllRoleDataSet();
            this.DropDownListRole.DataTextField = "roleName";
            this.DropDownListRole.DataValueField = "roleId";
            this.DropDownListRole.DataBind();


            this.GridViewUser.DataSource = staffDAL.GetAllUserInfo();
            this.GridViewUser.DataBind();




        }


        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            string userName = this.TextBoxCName.Text;

            int check = staffDAL.CheckLoginName(userName);
            if (check > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('用户-"+userName+"-已经存在！')</script>");
            }
            else
            {
                string eName = this.txtEName.Text;
                string pwd = md5.StringToMD5("000000");//初始密码6个0

                string sex = this.ddlSex.SelectedValue.ToString();
                int deptId = int.Parse(this.DropDownListDept.SelectedValue.ToString());
                int roleId = int.Parse(this.DropDownListRole.SelectedValue.ToString());
                string officeTel = this.txtOfficeTel.Text;
                string shortTel = this.txtShortTel.Text;
                string mobile = this.txtPhone.Text;
                string email = this.txtEmail.Text.Trim();
                string workCity = this.txtWorkCity.Text;

               
                int temp = staffDAL.InsertNewUserInfo(userName,pwd,deptId,roleId,englishName,sex,region,officeTel,extensionTel,mobile,email);
                if (temp > 0)
                {
                    UserInfo userInfo =Session["userInfo"]  as UserInfo;
                    string Ip = Request.UserHostAddress.ToString();
                    int t = logs.AddNewLogsInfo("insert new user--"+userName,userInfo.UserName,Ip);
                    this.BindDropDownList();

                    this.TextBoxLoginName.Text = "";
                    this.TextBoxCName.Text = "";
                    this.txtEName.Text = "";
                    this.txtOfficeTel.Text = "";
                    this.txtShortTel.Text = "";
                    this.txtPhone.Text = "";
                    this.txtEmail.Text = "";
                    this.txtWorkCity.Text = "";


                }

            }



        }

        protected void ButtonAlter_Click(object sender, EventArgs e)
        {
            int userId=int.Parse(this.TextBoxLoginName.Text);
            string eName = this.txtEName.Text;
            

            string sex = this.ddlSex.SelectedValue.ToString();
            int deptId = int.Parse(this.DropDownListDept.SelectedValue.ToString());
            int roleId = int.Parse(this.DropDownListRole.SelectedValue.ToString());
            string officeTel = this.txtOfficeTel.Text;
            string shortTel = this.txtShortTel.Text;
            string mobile = this.txtPhone.Text;
            string email = this.txtEmail.Text.Trim();
            string workCity = this.txtWorkCity.Text;
            string userName = this.TextBoxCName.Text;

            int temp = staffDAL.UpdateUserInfo(userId,userName, deptId, roleId, eName, sex, workCity, officeTel, shortTel, mobile, email);
            if (temp > 0)
            {
                this.BindDropDownList();
                UserInfo userInfo = Session["userInfo"] as UserInfo;

                string Ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("update user", userInfo.UserName, Ip);

                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('修改成功')</script>");
                this.TextBoxLoginName.Text = "";
                this.TextBoxCName.Text = "";
                this.txtEName.Text = "";
                this.txtOfficeTel.Text = "";
                this.txtShortTel.Text = "";
                this.txtPhone.Text = "";
                this.txtEmail.Text = "";
                this.txtWorkCity.Text = "";


            }





        }

        protected void GridViewUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewUser.PageIndex = e.NewPageIndex;
            this.BindDropDownList();

        }

        protected void GridViewUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)//点击Gridview行,选中/取消选中,当前行中的Checkbox
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");               

                int userId = int.Parse(e.Row.Cells[1].Text);
                string userName = e.Row.Cells[2].Text;
                string eName = e.Row.Cells[3].Text;
                string sex = e.Row.Cells[4].Text;
                int roleId = Convert.ToInt32(((Label)e.Row.FindControl("LabelRole")).Text);
                int deptId = Convert.ToInt32(((Label)e.Row.FindControl("LabelDept")).Text);
                string workCity = e.Row.Cells[7].Text;
                string officeTel = e.Row.Cells[8].Text; ;
                string shortTel = e.Row.Cells[9].Text; ;
                string mobile = e.Row.Cells[10].Text; ;
                string email = e.Row.Cells[11].Text;
               
                e.Row.Attributes.Add("onclick", "gvselchange('" + userId + "','" + userName + "','" + eName + "','"+sex+"','" + deptId + "','" + roleId + "','" +officeTel + "','" +shortTel + "','" +mobile + "','" + email + "','" + workCity + "')");

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
                }

            }
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            string id = this.TextBoxLoginName.Text;
            int temp = staffDAL.DeleteStaff(id);
            if (temp > 0)
            {
                this.BindDropDownList();
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("delete user", userInfo.UserName, Ip);
                ClientScript.RegisterStartupScript(this.GetType(), "c", "<script language=javascript>alert('删除成功')</script>");
                this.TextBoxLoginName.Text = "";
                this.TextBoxCName.Text = "";
                this.txtEName.Text = "";
                this.txtOfficeTel.Text = "";
                this.txtShortTel.Text = "";
                this.txtPhone.Text = "";
                this.txtEmail.Text = "";
                this.txtWorkCity.Text = "";


            }

        }

        protected void btnRePassword_Click(object sender, EventArgs e)
        {
            int userId = Convert.ToInt32(this.TextBoxLoginName.Text);
            string loginName = "000000";

            string paswd = md5.StringToMD5(loginName);//初始化密码为6个0

            int temp = staffDAL.ChangPassword(userId, paswd);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("update user's password", userInfo.UserName, Ip);
                ClientScript.RegisterStartupScript(this.GetType(), "d", "<script language=javascript>alert('密码初始化成功')</script>");
                this.TextBoxLoginName.Text = "";
                this.TextBoxCName.Text = "";
                this.txtEName.Text = "";
                this.txtOfficeTel.Text = "";
                this.txtShortTel.Text = "";
                this.txtPhone.Text = "";
                this.txtEmail.Text = "";
                this.txtWorkCity.Text = "";


            }

            

        }


        public string englishName { get; set; }

        public string extensionTel { get; set; }

        public string region { get; set; }
    }
}
